import type { GlobalConfig, GlobEnvConfig } from "#/config";
import { warn } from "@/utils/log";
// 注意：由于 build 目录为 node 脚本，此处仅支持相对路径引入不含 node 模块的文件，改为 from 'build/constants' 或 import { PROD_CONFIG_NAME } from '../../../build' 均会导致 Rollup 将 build 目录当做浏览器脚本编译并导致失败
import { PROD_CONFIG_NAME } from "../../../build/constants";

export const useGlobSetting = (): Readonly<GlobalConfig> => {
  const env: GlobEnvConfig = import.meta.env.DEV ? import.meta.env : Reflect.get(window, PROD_CONFIG_NAME);

  if (!/[a-zA-Z\_]*/.test(env.VITE_GLOB_APP_SHORT_NAME)) {
    warn("VITE_GLOB_APP_SHORT_NAME 变量只能是字符/下划线，请在环境变量中修改并重新运行。");
  }

  const glob: Readonly<GlobalConfig> = {
    title: env.VITE_GLOB_APP_TITLE,
    apiUrl: env.VITE_GLOB_API_URL,
    shortName: env.VITE_GLOB_APP_SHORT_NAME,
    urlPrefix: env.VITE_GLOB_API_URL_PREFIX,
    uploadUrl: env.VITE_GLOB_UPLOAD_URL,
    ws: env.VITE_GLOB_WS_URL,
    layerUrl: env.VITE_GLOB_MAP_URL,
    layerMaxLevel: env.VITE_GLOB_MAP_MAX_LEVEL,
    staticUrl: env.VITE_GLOB_STATIC_RESOURCE,
  };
  return glob;
};
